const app = getApp()
const db = wx.cloud.database();
const _ = db.command;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        form: {
            wt1: '',
            wt1zl: '',
            wt2: '',
            wt2gx: '',
            wt2name: '',
            wt3: '',
            wt3day: '',
            wt3year: '',
            wt4: '',
            wt4week: '',
            wt4year: '',
            wt4zl: '',
            wt5sc: '',
            wt5kw: '',
            wt5sw: [],
            wt5bl: '',
            wt5blday: '',
            wt5blyear: '',
            wt6: '',
            wt7: '',
            wt8: '',
            wt9: '',
            wt10: []
        },
        date:'',
        sw: [{
            val: "油炸食物"
        }, {
            val: "辛辣食物"
        }, {
            val: "霉变食物"
        }, {
            val: "腌制食物"
        }, {
            val: "熏烤食物"
        }, {
            val: "不吃或者极少吃"
        }],
        bs: [{
            val: "无"
        }, {
            val: "高血压"
        }, {
            val: "糖尿病"
        }, {
            val: "高血脂"
        }, {
            val: "冠心病"
        }, {
            val: "肺结核"
        }, {
            val: "慢性支气管炎"
        }, {
            val: "哮喘"
        }, {
            val: "尘肺"
        }, {
            val: "幽门螺杆菌感染"
        }, {
            val: "慢性萎缩性胃炎"
        }, {
            val: "胃溃疡"
        }, {
            val: "胃息肉"
        }, {
            val: "慢性溃疡性结肠炎"
        }, {
            val: "结肠腺瘤"
        }, {
            val: "甲亢"
        }, {
            val: "甲状腺瘤"
        }, {
            val: "肝血吸虫"
        }, {
            val: "乙肝"
        }, {
            val: "丙肝"
        }, {
            val: "肝硬化"
        }],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},

    //提交问卷按钮
    submit: function (e) {
        let openid = app.globalData.openid;
        let name = app.globalData.name;
        let type = app.globalData.type;
        let date=this.data.date;
        console.log(e.detail.value)
        this.data.form = e.detail.value
        //判断是否已全部填写
        const {wt1,wt2,wt3,wt4,wt5sc,wt5kw,wt5sw,wt6,wt7,wt8,wt9,wt10} = this.data.form
        /*if (wt1 == '' || wt2 == '' || wt3 == '' || wt4 == '' || wt5sc == '' || wt5kw == '' || wt5sw.length == 0 || wt6 == '' || wt7 == '' || wt8 == '' ||wt9 == '' || wt10.length == 0) {
            wx.showToast({
                title: '请回答完问题',
                icon: 'none'
            })
        } else */if (this.data.date == '') {
            wx.showToast({
                title: '请选择日期',
                icon: 'none'
            })
        } else {
            wx.cloud.callFunction({
                name: 'wenjuanget',
                data: {
                    type,
                    openid
                },
                success: res => {
                    console.log('获取问卷openid', res)
                    if (res.result.data.length != 0) {
                        wx.cloud.callFunction({
                            name: 'wenjuanupd',
                            data: {
                                type,
                                openid,
                                name,
                                date,
                                ...this.data.form
                            },
                            success: ress => {
                                console.log('问卷信息更新成功: ', ress)
                            },
                            fail: ress => {
                                console.log('问卷信息更新失败: ', ress)
                            }
                        })
                    } else {
                        wx.cloud.callFunction({
                            name: 'wenjuanadd',
                            data: {
                                type,
                                openid,
                                name,
                                date,
                                ...this.data.form
                            },
                            success: ress => {
                                console.log('添加新问卷信息: ', ress)
                            },
                            fail: ress => {
                                console.log('添加新问卷信息失败: ', ress)
                            }
                        })
                    }
                },
                fail: res => {
                    console.log('获取问卷openid失败', res)
                }

            })



            wx.reLaunch({
                url: '/pages/QRcode/QRcode',
            })
        }
    },
    //选择提交日期
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
    },

})